
// 人物移动 {"y":2280,"x":4820,"方向":0,"类型":"行走"}

// @ts-ignore
export async function execute(client_id, message_json) {

    let login_characters = G.Characters.get_current_login_characters(client_id);

    if (G.MapUtils.is_move(login_characters["id"])) {
        if (message_json["类型"] === "行走") {

            login_characters["地图数据"]["x"] = message_json["x"];
            login_characters["地图数据"]["y"] = message_json["y"];

            // 如果是队长 则判断队员是否需要移动, 同时通知地图内其他玩家队员的移动信息 
            if (login_characters["队伍"] !== 0 && login_characters["队长"]) {
                //队长当前移动到的位置
                let leader_x = message_json["x"];
                let leader_y = message_json["y"];

                let leader_map_id = login_characters["地图数据"]["编号"];
                let direction = message_json["方向"];

                let team_data = G.TeamUtils.get_team_data(login_characters["队伍"]);
                if (team_data !== undefined) {

                    for (let index = 0; index < team_data["成员"].length; index++) {
                        let team_characters_id = team_data["成员"][index];

                        //只操作队员
                        if (Number(team_characters_id) !== Number(login_characters["id"])) {

                            let team_characters = _CharactersData.get(team_characters_id);
                            let team_characters_map_id = team_characters["地图数据"]["编号"];
                            let team_characters_x = team_characters["地图数据"]["x"];
                            let team_characters_y = team_characters["地图数据"]["y"];

                            //计算与队长的距离
                            let distance = G.MapUtils.get_point_distance(team_characters_x, team_characters_y, leader_x, leader_y);
                            // 当前应当保持的距离 队员序号乘以队伍距离
                            let current_distance = team_distance * index;

                            // console.log(`坐标间距离 ${distance}`)
                            // console.log(`应当保持距离 ${current_distance}`)

                            if (team_characters_map_id !== leader_map_id) {
                                // console.log("不在一个地图")
                                G.MapUtils.move_characters_to_map(team_characters, leader_map_id, leader_x, leader_y, false);
                            } else if (distance >= (current_distance * 5)) {
                                // console.log("超出五倍距离")
                                //超出五倍距离直接重置队员的坐标到队长的位置
                                G.MapUtils.reset_characters_move(team_characters_id, leader_x, leader_y, direction);
                            } else if (distance >= current_distance) {
                                //大于规定的队伍距离则移动
                                // console.log("大于规定的队伍距离")
                                // 根据距离计算新的坐标
                                let new_position = G.MapUtils.calculate_follow_position(team_characters_x, team_characters_y, leader_x, leader_y, current_distance);
                                team_characters["地图数据"]["x"] = new_position.x;
                                team_characters["地图数据"]["y"] = new_position.y;
                                team_characters["地图数据"]["方向"] = direction;

                                let character_client_id = G.Characters.get_characters_client_id(team_characters_id);
                                if (character_client_id !== undefined) {
                                    //通知队员客户端人物移动
                                    ClientUtils.send_client_message(character_client_id, 1001, {
                                        "x": new_position.x,
                                        "y": new_position.y,
                                        "距离": 0,
                                        "id": team_characters_id
                                    });
                                }
                                //通知地图内所有玩家包括队长有人在移动
                                let map_data = G.MapUtils.get_map_data(login_characters["地图数据"]["编号"]);
                                map_data.forEach((value, key) => {
                                    let map_characters_id = value["id"];
                                    //玩家 不是当前队员 是主角色
                                    if (value["类型"] === "玩家" && Number(map_characters_id) !== Number(team_characters_id) && G.MapUtils.check_in_map(leader_map_id, value, team_characters)) {
                                        let characters_client_id = G.Characters.get_characters_client_id(map_characters_id);
                                        if (characters_client_id !== undefined) {
                                            //通知地图内其他玩家有人在移动
                                            let send_data = {
                                                "id": team_characters_id,
                                                "路径": {
                                                    "x": new_position.x,
                                                    "y": new_position.y,
                                                    "距离": 0
                                                }
                                            };
                                            ClientUtils.send_client_message(characters_client_id, 1008, send_data);
                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            }
            if (login_characters["队伍"] === 0 || (login_characters["队伍"] !== 0 && login_characters["队长"])) {
                // 遇怪检测
                G.MapMonster.check_characters_map_monster(login_characters);
            }
        }
    }
}


